//
//  NewOrderDetailViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/11.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewOrderDetailViewController.h"
#import "NewOrderDetailHeaderView.h"
#import "OrderDetailStore.h"
#import "OrderCenterList.h"
#import "NewOrderDetailSnCell.h"
#import "NewOrderDetailSectionView.h"
#import "OrderDetailModel.h"
#import "posOrderShipVOListModel.h"
#import <objc/runtime.h>
#import "NewOrderDetailFooterView.h"

char* const buttonKey = "buttonKey";

@interface NewOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource,NewOrderDetailSectionViewDelegate>

@property (nonatomic,strong) NewOrderDetailHeaderView *headerView;
@property (nonatomic,strong) NewOrderDetailFooterView *footerView;
@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic,strong) UITableView *tableView;


@end

@implementation NewOrderDetailViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorColor = BASECOLOR;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(0);
        }];
    }
    
    return _tableView;
}

- (NewOrderDetailHeaderView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[NewOrderDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 415)];
        
    }
    
    return _headerView;
}
    
- (NewOrderDetailFooterView *)footerView{
    
    if (!_footerView) {
        
        _footerView = [[NewOrderDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 102)];
        
    }
    
    return _footerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"订单详情"];
    
    [self loadData];

}


- (void)loadData{
    // 网络请求一
    OrderDetailStore *store = [[OrderDetailStore alloc] init];
    
    MJWeakSelf
    [store getOrderDetailWithId:[OrderCenterList sharedInstance].selectedModel.uid Success:^(OrderDetailModel *model) {
        
        self.headerView.model = model;
        self.listArr = [posOrderShipVOListModel mj_objectArrayWithKeyValuesArray:model.posOrderShipVOList];
        self.footerView.model = model;
        [self.tableView reloadData];
        
    } Failure:^(NSError *error) {
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 25;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    posOrderShipVOListModel *groupModel = self.listArr[section];
    NSInteger count = groupModel.isOpened?groupModel.snList.count - 1:0;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    posOrderShipVOListModel *groupModel = self.listArr[indexPath.section];
    NewOrderDetailSnCell *cell = [NewOrderDetailSnCell tempWithTableView:tableView];
    cell.sn = groupModel.snList[indexPath.row + 1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    posOrderShipVOListModel *groupModel = self.listArr[section];
    NewOrderDetailSectionView *sectionView = [[NewOrderDetailSectionView alloc] initWithSection:section];
    sectionView.model = groupModel;
    sectionView.delegate = self;
    sectionView.backgroundColor = UIColor.whiteColor;
    if (groupModel.isOpened) {
        
        [sectionView.moreBtn setImage:[UIImage imageNamed:@"orderDetail_close"] forState:UIControlStateNormal];
    }else{
        
        [sectionView.moreBtn setImage:[UIImage imageNamed:@"orderDetail_open"] forState:UIControlStateNormal];
    }
    
    if (section == 0) {
        
        sectionView.ifHiddenTopLine = YES;
    }else{
        
        sectionView.ifHiddenTopLine = NO;
    }
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 145;
}


- (void)onPreBtn:(UIButton *)btn{
    
    posOrderShipVOListModel *groupModel = self.listArr[btn.tag];
    groupModel.isOpened = !groupModel.isOpened;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
//    [self scrollTableToFoot:YES];
}

#pragma mark  - 滑到最底部
- (void)scrollTableToFoot:(BOOL)animated
{
    NSInteger s = [self.tableView numberOfSections];  //有多少组
    if (s<1) return;  //无数据时不执行 要不会crash
    NSInteger r = [self.tableView numberOfRowsInSection:s-1]; //最后一组有多少行
    if (r<1) return;
    NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1];  //取最后一行数据
    [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:animated]; //滚动到最后一行
}
@end
